package com.zrrd.jt_admin.manager.controller;


import com.zrrd.jt_admin.common.vo.RoleVo;
import com.zrrd.jt_admin.common.dto.SetRulesDto;
import com.zrrd.jt_admin.common.entity.Role;
import com.zrrd.jt_admin.common.entity.RoleRule;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.common.vo.RolePageVo;
import com.zrrd.jt_admin.manager.service.RoleRuleService;
import com.zrrd.jt_admin.manager.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhangyk
 * @since 2024-06-29
 */
@Api(tags = "角色管理-接口文档")
@ApiResponses({
        @ApiResponse(code = 500, message = "服务器内部错误，请联系管理人员！"),
        @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")
})
@RestController
@RequestMapping("/admin/role")
@Slf4j
public class RoleController {
    @Resource
    private RoleService roleService;

    @Resource
    private RoleRuleService roleRuleService;

    @ApiOperation(value = "角色列表分页查询",notes = "角色列表")
    //http://localhost:80891/admin/role
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") Integer page,
                                     @RequestParam(value = "limit",required = false) Integer limit) {
        if (limit == null){
            limit = 5;
        }
        RolePageVo rolePageVo = roleService.findByPage(page, limit);
        return ResponseResult.queryResponse(rolePageVo);
    }

    @ApiOperation(value = "查询单个角色",notes = "查询单个角色")
    @GetMapping("/{id}/one")
    @ResponseBody
    public ResponseResult findOne(@PathVariable("id") Integer id) {
        RoleVo roleVo = roleService.findById(id);
        return ResponseResult.queryResponse(roleVo);
    }

    @ApiOperation(value = "增加角色",notes = "增加角色")
    //http://localhost:80891/admin/role/
    @PostMapping("/")
    @ResponseBody
    public ResponseResult addRole(@RequestBody Role role) {
        if(role==null){
            return ResponseResult.queryResponse(false);
        }
        boolean flag = roleService.save(role);
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "修改角色",notes = "修改角色")
    //http://localhost:80891/admin/role/
    @PutMapping("/")
    @ResponseBody
    public ResponseResult updateRole(@RequestBody Role role) {
        if(role==null){
            return ResponseResult.queryResponse(false);
        }
        return ResponseResult.updateResponse(roleService.updateById(role));
    }

    @ApiOperation(value = "删除角色",notes = "删除角色")
    @DeleteMapping("/{id}/delete")
    @ResponseBody
    public ResponseResult delete(@PathVariable("id") Integer id) {
        boolean flag = roleService.removeById(id);
        return ResponseResult.removeResponse(flag);
    }

    @ApiOperation(value = "修改状态", notes = "根据id修改状态")
    @PutMapping("/role/{id}/update_status")
    @ResponseBody
    public ResponseResult setStatus(@PathVariable("id") Integer id,Integer status) {
        Boolean bStatus=status==1?true:false;

        boolean flag = roleService.lambdaUpdate()
                .eq(Role::getId, id)
                .set(Role::getStatus, bStatus)
                .update();

        /*
            UpdateWrapper<Manager> uw=new UpdateWrapper<>();
            uw.eq("id",id);
            uw.set("status",status);
            boolean flag = managerService.update(uw);
        */

        return ResponseResult.updateResponse(flag);
    }




    @ApiOperation(value = "配置权限",notes = "给角色配置权限")
    @PostMapping("/set_rules")
    @ResponseBody
    public ResponseResult setRules(@RequestBody SetRulesDto setRulesDto) {
        if (setRulesDto == null) {
            return ResponseResult.updateResponse(false);
        }
        Role role = roleService.getById(setRulesDto.getId());
        //System.out.println(role);
        if (role == null) {
            return ResponseResult.updateResponse(false);
        }
        for (Integer ruleId : setRulesDto.getRuleIds()) {
            //查询role_rule表中是否存在该权限
            RoleRule rr = roleRuleService.lambdaQuery()
                    .eq(RoleRule::getRoleId, role.getId())
                    .eq(RoleRule::getRuleId, ruleId)
                    .one();
            if (rr != null) {
                continue;
            }
            RoleRule roleRule = new RoleRule();
            roleRule.setRoleId(role.getId());
            roleRule.setRuleId(ruleId);
            //System.out.println(roleRule);
            roleRuleService.save(roleRule);
        }
        return ResponseResult.updateResponse(true);
    }
}

